using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using ExchangeHog.Pop3Client.Responses;

namespace ExchangeHog.Pop3Client.Commands
{
	internal sealed class TopCommand : Pop3Command<RetrResponse>
	{
		private int messageNumber;
		private int lineCount;

		internal TopCommand(Stream stream, int messageNumber, int lineCount)
			: base(stream, true, Pop3State.Transaction)
		{
			if (messageNumber < 1)
				throw new ArgumentOutOfRangeException("messageNumber");

			if (lineCount < 0)
				throw new ArgumentOutOfRangeException("lineCount");

			this.messageNumber = messageNumber;
			this.lineCount = lineCount;
		}

		protected override byte[] CreateRequestMessage()
		{
			return GetRequestMessage(Pop3Commands.Top, messageNumber.ToString(), " ", lineCount.ToString(), Pop3Commands.Crlf);
		}

		protected override RetrResponse CreateResponse(byte[] buffer)
		{
			Pop3Response response = Pop3Response.CreateResponse(buffer);

			return response == null?
				null:
				new RetrResponse(response, GetResponseLines(StripPop3HostMessage(buffer, response.HostMessage)));
		}
	}
}
